package com.muyu.fence.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.fence.domain.database.ElectronicFence;
import com.muyu.fence.domain.req.ElectroicFenceAddReq;
import com.muyu.fence.domain.req.ElectroicFenceListReq;
import com.muyu.fence.domain.resp.ElectronicFenceResp;
import com.muyu.fence.mapper.ElectronicFenceMapper;
import com.muyu.fence.service.ElectronicFenceService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @Author：yuping
 * @Package：com.muyu.fence.service.impl
 * @Project：cloud-server
 * @name：ElectronicFenceServiceImpl
 * @Date：2024/9/17 19:31
 */
@Log4j2
@Service
public class ElectronicFenceServiceImpl extends ServiceImpl<ElectronicFenceMapper, ElectronicFence> implements ElectronicFenceService {

    @Autowired
    private ElectronicFenceMapper electronicFenceMapper;


    @Override
    public List<ElectronicFenceResp> fenceselectList(ElectroicFenceListReq electroicFenceListReq) {

        LambdaQueryWrapper<ElectronicFence> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(
                StringUtils.isNotNull(electroicFenceListReq.getName()),ElectronicFence::getName, electroicFenceListReq.getName()
        );
      queryWrapper.eq(
              StringUtils.isNotEmpty(electroicFenceListReq.getFenceType()),ElectronicFence::getFenceType, electroicFenceListReq.getFenceType()
       );
        queryWrapper.eq(
                StringUtils.isNotEmpty(electroicFenceListReq.getStatus()),ElectronicFence::getStatus, electroicFenceListReq.getStatus()
        );
        List<ElectronicFence> list = this.list(queryWrapper);

        return list.stream().map(ElectronicFence::bullerResp).toList();
    }

    @Override
    public void AddFence(ElectroicFenceAddReq electroicFenceAddReq) {



         electronicFenceMapper.insert(ElectronicFence.buildElectroicAdd(electroicFenceAddReq));




    }

    @Override
    public ElectronicFence findElectronicByid(Long id) {

        ElectronicFence electronicFence = electronicFenceMapper.selectById(id);
        return electronicFence;
    }

    @Override
    public void delElectronById(Long id) {
        electronicFenceMapper.deleteById(id);
    }


    @Override
    public void openFence(Long id) {

        updateStatus(id,0);

    }

    @Override
    public void closeFence(Long id) {
        updateStatus(id,1);
    }

    public void updateStatus (Long id,int status){
        UpdateWrapper<ElectronicFence> updateWrapper = new UpdateWrapper<>();

        updateWrapper.eq("id",id).set("status",status);

        this.update(updateWrapper);


    }


    @Override
    public void setFenceWay(Long id, String longitudeLatitude) {

        UpdateWrapper<ElectronicFence> updateWrapper = new UpdateWrapper<>();

        updateWrapper.eq("id",id).set("longitude_latitude",longitudeLatitude);

        this.update(updateWrapper);
    }

    @Override
    public List<ElectronicFenceResp> fenceArray() {

        LambdaQueryWrapper<ElectronicFence> queryWrapper = new LambdaQueryWrapper<>();

        LambdaQueryWrapper<ElectronicFence> wrapper = queryWrapper.eq(ElectronicFence::getStatus, 0);

        List<ElectronicFence> list = this.list(wrapper);

        return list.stream().map(ElectronicFence::bullerResp).toList();
    }

    @Override
    public void unquireFence(String name) {
        LambdaQueryWrapper<ElectronicFence> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(ElectronicFence::getName,name);

        List<ElectronicFence> fenceList = this.list(queryWrapper);

        if (fenceList.size()>0){
            throw new RuntimeException("电子围栏名不能重复");
        }


    }

    @Override
    public List<ElectronicFenceResp> selectListByIds(List<Long> ids) {

        List<ElectronicFence> electronicFenceList = this.list(Wrappers.<ElectronicFence>lambdaQuery().in(ElectronicFence::getId, ids));


        return  electronicFenceList.stream().map(ElectronicFence::bullerResp).toList();
    }
}
